$(function () {

    //生成 模拟按钮
    $("[data-mock-btns]").each(function () {
        var $this = $(this);
        var input = $this.prev().find("input");
        var name = input.attr("name");
        var genTypes = $this.data("mockBtns");
        log(genTypes);
        var btnGroup = buildMockBtnGroup(name, genTypes);
        $this.html(btnGroup);
        $this.addClass("mock-btns");
    });

});

/**
 * 生成业务是数据
 * @param json{字段：类型}
 * @type 类型：cname、ctitle、idcard、uuid、city、province、email、ip、domain、url、datetime、date、int、long、float
 */
function genBizData(json){
    for(var name in json){//遍历json对象的每个key/value对,p为key
        log(name + " " + json[name]);
        $("[name="+name+"]").val(getMockData(json[name]));
    }
}

function buildMockBtnGroup(name, types){
    var arr = types.split(",");
    var str = new StringBuffer();
    for(var i=0; i<arr.length; i++){
        str.append('<a href="javascript:setMockDataToInput(\''+name+'\', \''+arr[i]+'\');">'+getMockBtnText(arr[i])+'</a>');
    }
    return str.toString();
}

function setMockDataToInput(name, type){
    $("[name="+name+"]").val(getMockData(type));
}

function getMockData(type){
    var val;
    if(type=="cname")
        val = Mock.Random.cname();
    else if(type=="idcard")
        val = Mock.Random.id();
    else if(type=="uuid")
        val = Mock.Random.uuid();
    else if(type=="city")
        val = Mock.Random.city();
    else if(type=="province")
        val = Mock.Random.province();
    else if(type=="email")
        val = Mock.Random.email();
    else if(type=="ip")
        val = Mock.Random.ip();
    else if(type=="domain")
        val = Mock.Random.domain();
    else if(type=="url")
        val = Mock.Random.url();
    else if(type=="ctitle")
        val = Mock.Random.ctitle();
    else if(type=="datetime")
        val = Mock.Random.datetime();
    else if(type=="date")
        val = Mock.Random.date();
    else if(type=="int")
        val = Mock.Random.integer(60, 8000);
    else if(type=="long")
        val = Mock.Random.integer(1000, 1000000);
    else if(type=="float")
        val = Mock.Random.float(10, 1000, 2, 2);
    else
        val = Mock.Random.word(8);
    return val;
}

function getMockBtnText(type){
    var val;
    if(type=="cname")
        val = "姓名";
    else if(type=="idcard")
        val = "身份证";
    else if(type=="uuid")
        val = "uuid";
    else if(type=="city")
        val = "城市";
    else if(type=="province")
        val = "省份";
    else if(type=="email")
        val = "邮箱";
    else if(type=="ip")
        val = "ip";
    else if(type=="domain")
        val = "域名";
    else if(type=="url")
        val = "网址";
    else if(type=="ctitle")
        val = "标题";
    else if(type=="datetime")
        val = "时间";
    else if(type=="date")
        val = "日期";
    else if(type=="int")
        val = "整数";
    else if(type=="long")
        val = "长整数";
    else if(type=="float")
        val = "浮点数";
    else
        val = "英文";
    return val;
}